package org.fastiot.gateway

import grails.databinding.BindUsing
import org.fastiot.protocol.Packet

/**
 * 基于字节码的通讯报文
 * Bytecode-based message
 */
class Message {

    static mapWith = "mongo"

    Date dateReceived

    @BindUsing({ obj, source ->
        Packet.Direction.byValue(Integer.parseInt(source["direction"] as String))
    })
    Packet.Direction direction

    Map<String, Serializable> metadata
    static embedded = ['metadata']
    byte[] contents

    Date dateCreated

    static constraints = {
        dateReceived(nullable: false)
        direction(nullable: false, inList: Packet.Direction.values())
        metadata(nullable: false)
        contents(nullable: false)
        dateCreated()
    }

    static mapping = {
        dateReceived index: true, indexAttributes: [expireAfterSeconds: 60 * 60 * 24]
    }
}
